package com.zrf.service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zrf.dao.impl.FlowDaoImpl;
import com.zrf.dao.impl.LeaveDaoImpl;
import com.zrf.dao.impl.PersonDaoImpl;
import com.zrf.pojo.Flow;
import com.zrf.pojo.Leave;
import com.zrf.pojo.Person;

@Service("leaveService")
public class LeaveService {

	@Autowired
	private PersonDaoImpl pdi  = null;
	@Autowired
	private LeaveDaoImpl ldi = null;
	@Autowired
	private FlowDaoImpl fdi = null;
	
	
	public boolean leave(String name){
		boolean is = false;
		Timestamp ts = new Timestamp((new Date()).getTime());
		try {
			Person p = pdi.findByName(name);
			Leave l = new Leave(p,2,0);
			Flow f = new Flow(pdi.findById(p.getPid()), l, ts,0);
			ldi.add(l);
			fdi.add(f);
			is = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return is;
	}
	public boolean flow(String name, Integer state, Integer id) {
		// TODO Auto-generated method stub
		boolean is = false;
		Timestamp ts = new Timestamp((new Date()).getTime());
		try {
		    Person p = pdi.findByName(name);
			Flow f = fdi.findById(id);
			Leave l = ldi.findById(f.getLeave().getLid());
			if ((p.getPgrade()==1||p.getPgrade()==2)&&state==0) {
				f.setFstate(state);
				f.setFdate(ts);
			    fdi.update(f);
				f = new Flow(pdi.findByType(1), l,ts,0);
				fdi.add(f);
			}else if (p.getPgrade()==2||state==1) {
				f.setFstate(state);
				f.setFdate(ts);
				l.setLstate(state);
				fdi.update(f);
				ldi.update(l);
			}
			is = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return is;
	}
	
	public List<?> findMyLeave(String name){
		return ldi.findByName(name);
	}
	
	public List<?> findOtherLeave(String name, Integer state){
		return fdi.findByState(name, state);
	}
	public List<?> NofindOtherLeave(String name, Integer noState){
		return fdi.findByNoState(name, noState);
	}
}
